<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/login.css" />
    <!-- <link rel="stylesheet" type="text/css" href="../css/common.css" /> -->
</head>

<body>
    <div id="wrap">
      <div id="header">
              <h1>SWMS</h1>
          </div>
          <div id="main" class="flex-con">
            <div class="login_box">
              <div class="phone">
                  <input type="text" placeholder="User" id="tel">
              </div>
              <div class="password">
                  <input type="password" placeholder="Password" id="password">
                  <!-- <div class="find" tapmode onclick="fnForgetPassword()">找回密码</div> -->
              </div>
              <button class="login"  tapmode onclick="fnLogin()">Login</button>
            </div>
        <!-- <div class="register open-win" win="register_mobile">注册账号</div> -->
      </div>
    </div>
</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/common.js"></script>
<script type="text/javascript">
    apiready = function() {
  //     var query = $api.getStorage('query');
  //     if (typeof query !== 'undefined') {
  //     if (query.Org) {
  //       api.openFrame({
  //         name: 'main',
  //         url: './html/main.html',
  //         rect: {
  //             x: 0,
  //             y: 0,
  //             w: 'auto',
  //             h: 'auto'
  //         }
  //     });
  //     api.closeFrame();
  //   }
  // }
        fnInit();
        fnReady();
    };
  var tel, password, emailText, username;
   function fnInit() {
       username = $api.byId('tel');
       password = $api.byId('password');
   };

       function fnLogin() {
          emailText = username.value,
          passwordText = password.value;
          if (emailText === '') {
            api.toast({
               msg: 'enter one user name',
               duration: 2000,
               location: 'middle'
             });
            return false;
          }
          if (passwordText === '') {
            api.toast({
               msg: 'Please input a password',
               duration: 2000,
               location: 'middle'
             });
            return false;
          }
          api.showProgress({
             title: 'Login',
             modal: true,
             text: 'One moment please..'
           });
           password.blur();
           api.ajax({
               url: 'http://223.255.10.50:8029/dsp-ShateApp-ui/sysUserLogin/app/login.action',
               method: 'post',
               dataType: 'json',
               returnAll: false,
               data: {
                 values: {
                   dlzh: emailText,
                   pwd: passwordText
                 }
               }
           }, function(ret, err) {
             if(err) {
              api.hideProgress();
              alert('Server exception');
              // api.alert({ msg: JSON.stringify(err.msg) });
              return false;
             }
            //  console.log(JSON.stringify(ret))
             if (ret.code === '1') {
               let query = {};
               let maps = {};
               for (var key in ret.data) {
                 query = {
                   Org: ret.data[0].OrgId,
                   id: ret.data[0].userid
                 }
                 maps = {
                   X: ret.data[key].center_x,
                   Y: ret.data[key].center_y,
                   zoom: ret.data[key].map_zoom
                 }
               }
               $api.setStorage('query', query);
               $api.setStorage('maps', maps);
               api.hideProgress();
              //  api.openWin({
              //     name: 'main',
              //     url: './html/main.html'
              // });
               api.openFrame({
                 name: 'main',
                 url: './html/main.html',
                 rect: {
                     x: 0,
                     y: 0,
                     w: 'auto',
                     h: 'auto'
                 }
             });
             api.closeFrame();
            //  console.log($api.getStorage('userid');
            //  api.closeWin();
            } else {
              api.hideProgress();
              alert('ERROR Incorrect username or password');
              username.value = '';
              password.value = '';
            }
           });
       };

   function fnChange(userName){
       username.value=userName;
   }
</script>

</html>
